import Flutter
import UIKit
import AVFoundation

public class IosMicrophonePermissionPlugin: NSObject, FlutterPlugin {
    public static func register(with registrar: FlutterPluginRegistrar) {
        let channel = FlutterMethodChannel(name: "ios_microphone_permission", binaryMessenger: registrar.messenger())
        let instance = IosMicrophonePermissionPlugin()
        registrar.addMethodCallDelegate(instance, channel: channel)
    }
    
    public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        switch call.method {
        case "getMicrophonePermission":
            result(AVCaptureDevice.authorizationStatus(for: AVMediaType.audio).rawValue)
        case "requestMicrophonePermission":
            AVAudioSession.sharedInstance().requestRecordPermission { (granted) in
                //此处可以判断权限状态来做出相应的操作，如改变按钮状态
                if granted{
                    result(true)
                } else {
                    result(false)
                }
            }
        default:
            result(FlutterMethodNotImplemented)
        }
    }
}
